﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.CustomerManage
{
    public partial class Review : Common.PageBase
    {
        private T_SPECIALLIST_BLL specialListBll = new T_SPECIALLIST_BLL();
        T_BLACKAPPLY_BLL bll = new T_BLACKAPPLY_BLL();
        private T_CUSTOMER_BLL customerBll = new T_CUSTOMER_BLL();
        T_DICTIONARY_BLL dBll = new T_DICTIONARY_BLL();


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel);
            PageHelper.SetPopCalender(this.Page, txtCREATETIME);
            PageHelper.SetPopCalender(this.Page, txtREVIEWTIME);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                ddlSTATUSBinding();
                ddlCALLTYPEBinding();


                pageDataInit();
            }
        }

 
        #region 页面初始化数据绑定


        private void ddlCALLTYPEBinding()
        {
            EnumHelper.FillListControl(ddlCALLTYPE, typeof(WorkOrderType));
        }

        private void ddlSTATUSBinding()
        {
            EnumHelper.FillListControl(ddlSTATUS, typeof(BlackApplyStatus));
        }



        #endregion

        private void pageDataInit()
        {
            string _autoid = String.IsNullOrEmpty(Request.QueryString["AUTOID"]) ? "" : Request.QueryString["AUTOID"].Trim();

            if (!String.IsNullOrEmpty(_autoid))
            {
                T_BLACKAPPLY_Entity entity = bll.GetEntity("AUTOID", _autoid);

                PageHelper.SetControlValue<T_BLACKAPPLY_Entity>(this.Page, entity);
                txtCREATETIME.Text = entity.CREATETIME.HasValue
                                         ? entity.CREATETIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
                txtREVIEWTIME.Text = entity.REVIEWTIME.HasValue
                                         ? entity.REVIEWTIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
                PageHelper.SetControlEnabled(this.Page, false);
                txtREVIEWIDEA.Enabled = true;
                if(entity.STATUS.ToString() != ((int)BlackApplyStatus.待审核).ToString())
                {
                    btnPass.Visible = false;
                    btnReject.Visible = false;
                    btnInvalid.Visible = false;
                }

                if (entity.CREATEPEOPLEID.ToString() != _userLoginInfo.UserEntity.USERID.ToString())
                {
                    btnInvalid.Visible = false;
                }
            }


        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string _autoid = String.IsNullOrEmpty(Request.QueryString["AUTOID"]) ? "" : Request.QueryString["AUTOID"].Trim();


            T_BLACKAPPLY_Entity entity = bll.GetEntity("AUTOID", _autoid);

            if (entity != null)
            {
                PageHelper.GetControlValue<T_BLACKAPPLY_Entity>(this.Page, ref entity);
                entity.REVIEWTIME = DateTime.Now;
                entity.REVIEWPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.REVIEWPEOPLEID = _userLoginInfo.UserEntity.USERID;

                Button btn = sender as Button;
                switch (btn.ID)
                {
                    case "btnPass":
                        {
                            T_CUSTOMER_Entity customerEntity = customerBll.GetEntity("CUSTOMERID", entity.CUSTOMERID);
                            if (customerEntity == null)
                            {
                                customerEntity = customerBll.NewCustomerWithDefaultVale(_userLoginInfo);
                                customerEntity.SPECIALTYPE = ((int)CustomerSpecialType.黑名单).ToString();
                                if (entity.PHONE.Length < 11)
                                {
                                    customerEntity.HOMEPHONE = entity.PHONE;
                                }
                                else
                                {
                                    customerEntity.MOBILEPHONE = entity.PHONE;
                                }
                                customerBll.Insert(customerEntity);
                            }
                            else
                            {
                                customerEntity.SPECIALTYPE = ((int) CustomerSpecialType.黑名单).ToString();
                                customerBll.Update(customerEntity);
                            }
                            specialListBll.UpdateSpecialList(customerEntity, CustomerSpecialType.黑名单, _userLoginInfo, entity.PHONE);

                            entity.STATUS = (int) BlackApplyStatus.审核通过;
                        }
                        break;
                    case "btnReject":
                        {
                            entity.STATUS = (int)BlackApplyStatus.审核驳回;
                        }
                        break;
                    case "btnInvalid":
                        {
                            entity.STATUS = (int)BlackApplyStatus.作废;
                        }
                        break;
                }

                long rel = bll.Update(entity);
                if (rel > 0)
                {
                    PageHelper.ShowMessage(this.Page, ((BlackApplyStatus) Enum.Parse(typeof (BlackApplyStatus), entity.STATUS.ToString())).ToString() + "成功！", "list.aspx");
                }
                else
                {
                    PageHelper.ShowMessage(this.Page, ((BlackApplyStatus)Enum.Parse(typeof(BlackApplyStatus), entity.STATUS.ToString())).ToString() + "失败！");
                }
            }
            
        }
    }
}
